package com.tgh.gles2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.util.ArrayList;
import java.util.Arrays;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.test.AndroidTestCase;
import android.util.Log;

import com.tgh.utils.GLFrame;
import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLTools;
import com.tgh.utils.Math3D;
import com.tgh.utils.Matrix44f;
import com.tgh.utils.Vector2f;
import com.tgh.utils.Vector3f;

public class Test extends AndroidTestCase {
	
	public void testNormal() {
		float vertices[] = 
            {
                    -1.0f,  1.0f, 1.0f,
                     1.0f,  1.0f, 1.0f,
                     1.0f, -1.0f, 1.0f,
                    -1.0f, -1.0f, 1.0f,
                     
                    -1.0f,  1.0f,-1.0f,
                     1.0f,  1.0f,-1.0f,
                     1.0f, -1.0f,-1.0f,
                    -1.0f, -1.0f,-1.0f
            }; 
		 byte indices[] =                  
             {
                 1,0,3,
                 1,3,2,
                 1,2,6,
                 1,6,5,
                 1,5,4,
                 1,4,0,
                 7,4,5,
                 7,5,6,
                 7,6,2,
                 7,2,3,
                 7,3,0,
                 7,0,4
                 
             };
		 float[] normals = GLTools.generateNormalsForTriangleList(vertices, indices);
		 Log.i("test", Arrays.toString(normals));
		 
		float[][] vPoints = new float[][] {
				{ -1.0f, 1.0f, 1.0f },
				{ 1.0f, 1.0f, 1.0f },
				{ 1.0f, -1.0f, 1.0f },
				{ -1.0f, -1.0f, 1.0f },

				{ -1.0f, 1.0f, -1.0f },
				{ 1.0f, 1.0f, -1.0f },
				{ 1.0f, -1.0f, -1.0f },
				{ -1.0f, -1.0f, -1.0f }
		};
		byte[][] bIndices=new byte[][]{
				{1,0,3},
                {1,3,2},
                {1,2,6},
                {1,6,5},
                {1,5,4},
                {1,4,0},
                {7,4,5},
                {7,5,6},
                {7,6,2},
                {7,2,3},
                {7,3,0},
                {7,0,4}
		};
		normals = GLTools.generateNormalsForTriangleList(vPoints, bIndices);
		 Log.i("test", Arrays.toString(normals));
	}
	
	public void testRotate() {
	    Matrix44f matrix=new Matrix44f();
	    matrix.loadRotate(30, 1, 0, 0);
	    Vector3f vect=new Vector3f(10, 10, 10);
	    Vector3f result=new Vector3f();
        Math3D.rotateVector3(result, vect, matrix);
    }
	
	public void testMult() {
        Matrix44f rot=new Matrix44f();
        rot.loadRotate(30, 1,0,0);
        Matrix44f translate=new Matrix44f();
        translate.loadTranslate(20, 20, 20);
        rot.multiply(translate);
        Log.i("test", Arrays.toString(rot.getArray()));
        rot.loadRotate(30, 1,0,0);
        rot.translate(20, 20, 20);
        Log.i("test", Arrays.toString(rot.getArray()));
        
        float[] rm=new float[16];
        Matrix.setRotateM(rm, 0, 30, 1, 0, 0);
        Log.i("test", Arrays.toString(rm));
        float[] tm=new float[16];
        Matrix.setIdentityM(tm, 0);
        Matrix.translateM(tm, 0, 20, 20, 20);
        Log.i("test", Arrays.toString(tm));
        float[] result=new float[16];
        Matrix.multiplyMM(rm, 0, rm, 0, tm, 0);
        Log.i("test", Arrays.toString(rm));
    }
	
	public void translate(short[] triangleIndices) {
	    short[] lineVertices=new short[triangleIndices.length*2];
        int triangleCount=triangleIndices.length/3;
        int index=0;
        for(int i=0;i<triangleCount;i++){
            short v1=triangleIndices[i*3+0];
            short v2=triangleIndices[i*3+0];
            short v3=triangleIndices[i*3+0];
            lineVertices[index++]=v1;
            lineVertices[index++]=v2;
            lineVertices[index++]=v2;
            lineVertices[index++]=v3;
            lineVertices[index++]=v3;
            lineVertices[index++]=v1;
        }
    }
	
	public void testBatch() {
	    GLTools.makeTorus(0.5f, 0.2f, 52, 52);
//	    writeToSD(), mVertices);
//        writeToSD(new File("mnt/sdcard/torusN.txt"), mNormals);
//        writeToSD(new File("mnt/sdcard/torusT.txt"), mTextures);
//        writeToSD(new File("mnt/sdcard/torusI.txt"), mIndices);
        ArrayList<Vector3f> vertices=readFromSD(new File("mnt/sdcard/torusV.txt"));
        Log.i("test", ""+vertices.size());
        ArrayList<Vector3f> normals=readFromSD(new File("mnt/sdcard/torusN.txt"));
        Log.i("test", ""+normals.size());
        ArrayList<Vector2f> textures=readFromSD(new File("mnt/sdcard/torusT.txt"));
        Log.i("test", ""+textures.size());
        ArrayList<Short> indices=readFromSD(new File("mnt/sdcard/torusI.txt"));
        Log.i("test", ""+indices.size());
        
    }
	
	public <T> ArrayList<T> readFromSD(File file) {
        try {
            FileInputStream fis=new FileInputStream(file);
            ObjectInputStream ois=new ObjectInputStream(fis);
            ArrayList<T> arrays=(ArrayList<T>) ois.readObject();
            return arrays;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	public void testInvert() {
        float[] mat=new float[16];
        Matrix.setRotateM(mat, 0, 30, 1, 0, 0);
        Log.i("test", "mat="+Arrays.toString(mat));
        float[] m=new float[16];
        Matrix.invertM(m, 0, mat, 0);
        Log.i("test", "mat="+Arrays.toString(mat));
        Log.i("test", "m="+Arrays.toString(m));
    }
}
